package hello;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
/**
 *
 * @author THANH PHUC
 */
public class Monster extends Sprite{
    static final int WIDTH = 30;
    static final int HEIGHT = 29;
    
    public int direction;
     static int seek =0;
    public int Action;
    public int speed;
    
    public Monster(int x, int y,String Imagine)throws Exception
    {
        
      
      
          super(Image.createImage(Imagine), WIDTH, HEIGHT);
          
        //Image a = Image.createImage("/images/cowboy.png");
        //super(a,WIDTH,HEIGHT);
        int myInitialX = x;
        int myInitialY = y;
        // we define the reference pixel to be in the middle 
        // of the cowboy image so that when the cowboy turns 
        // from right to left (and vice versa) he does not 
        // appear to move to a different location.
        defineReferencePixel(WIDTH/2, 0);
        setRefPixelPosition(myInitialX, myInitialY);
        //setFrameSequence(FRAME_SEQUENCE);
            speed = 3;
    //   defineCollisionRectangle(5, 15, 3, 10);
       
    }
  static  int GetRandomInt()
    {
       
        try
        {
       Random r = new Random(seek);
        int retVal =  r.nextInt() % 4;
        if(retVal <= 0) {
        retVal += 4;
        
        seek ++;
        if (seek >100)
            seek =0;
        }
        if (retVal ==0)
            retVal =1;
         return(retVal);
        }
        catch(Exception ex)
        {
            throw null;
        }
       
    }
    private static  int a = 1;
    void advance() {
        if(a % 40 == 0)
        {
            direction = GetRandomInt();
        }
        Action = direction;
        if(direction == 1)
        {
            setTransform(TRANS_NONE);
            move(speed,0);
            nextFrame();
        }
        if(direction == 2)
        {
            setTransform(TRANS_MIRROR);
            
            move(-speed,0);
            nextFrame();
        }
        if(direction == 3)
        {
            move(0,-speed);
            nextFrame();
        }
        if(direction == 4)
        {
            move(0,speed);
            nextFrame();
        }
        int x = getX();
        int y = getY();
        //boolean b = collidesWith(jump_manager.background,false);
        
        if (x <0||x > JumpManager.DISP_WIDTH-20)
        {
            GetBack();
        }
        if (y <0 ||y > JumpManager.DISP_HEIGHT-35)
        {
          GetBack();
        }
         

        a++;
    }
    public void GetBack()
    {
         if(Action == 1)
        {
           // setTransform(TRANS_NONE);
            move(-speed,0);
          //  nextFrame();
        }
        if(Action == 2)
        {
           // setTransform(TRANS_MIRROR);
            
            move(speed,0);
            nextFrame();
        }
        if(Action == 3)
        {
            move(0,speed);
            nextFrame();
        }
        if(Action == 4)
        {
            move(0,-speed);
            nextFrame();
        }
        
    }
}
